//生成的都是虚拟的dom对象
export function createElement(vm,tag,data={},...children){

   if(isHtmlTag(tag)){
      return vnode(vm,tag,data,data.key,children,undefined)
   }else{
       let ctor=vm.$options.components[tag]
       return createComponent(vm,tag,data,data.key,children,ctor)
   }
   
}

export function createComponent(vm,tag,data,key,children,ctor){
    
    if(typeof ctor ==='object'){
        //创建组件
        ctor=vm.$options._base.extend(ctor)
    }
    data.hooks={
        init(vnode){
            //实例化组件
            let child=ctor.componentInstance=new ctor({isComponent:true})
            //组件挂载
            child.$mount()
        }
    }
    return vnode(vm,`vue-componnet-${ctor.cid}-${tag}`,data,key,undefined,undefined,{ctor,children})
}

export function createTextElement(vm,text){
   return vnode(vm,undefined,undefined,undefined,undefined,text,undefined)
}

//虚拟dom对象
function vnode(vm,tag,data,key,children,text,componentInstance){
    return {
        vm,tag,data,key,children,text,componentInstance
    }
}

export function isHtmlTag(tag){
    let html=['div','span','a','p','input']
    return html.includes(tag)
 }